/**
 * @file
 * @auth jinguangguo
 * @date 2017/1/18
 */

import Vue from 'vue';
import tpl from './tpl.html';
import Qrcode from '../../../../../grsc-base/component/Qrcode.vue';
import Loading from '../../../../../grsc-base/component/Loading.vue';
import NoData from '../../../../../grsc-base/component/NoData.vue';
import filter from '../../../../../grsc-base/util/filter';
import lib from '../../../../../src/common/lib';
import service from "../../service";
import commonService from "../../../../common/commonService";

const DEFAULT_MAX_PAGE_SIZE = 100;

export default Vue.extend({
    name: "inviteInfo",
    template: tpl,
    components: {
        Qrcode,
        Loading,
        NoData
    },

    data() {
        return {
            filter,
            isLoading: true,
            info: {
                inviteUserRewardRecords: [],
                inviteActivityRewardConfigs: []
            },
            inviteLink: '',
            recommendList: [],
            rewardList: []
        }
    },
    created() {
        let that = this;

        if (lib.isLogin() === false) {  // 如果没有登录，显示默认的假数据即可
            this._setDefaultData();
            this.isLoading = false;
        } else {
            this.isLoading = true;
            $.when(service.infoQuery(), service.recommendQuery({
                pageNo: 1,
                pageSize: DEFAULT_MAX_PAGE_SIZE
            }), service.rewardQuery({
                pageNo: 1,
                pageSize: DEFAULT_MAX_PAGE_SIZE
            })).done((infoQueryRep, recommendQueryRep, rewardQueryRep) => {
                this.isLoading = false;
                doInfoQueryRep(infoQueryRep);
                doRecommendQueryRep(recommendQueryRep);
                doRewardQueryRep(rewardQueryRep);
            });
        }

        function doInfoQueryRep(rep) {
            if (rep.code === window.CODE.SUCCESS) {
                let info = rep.data;
                // 生成link
                that._setInviteLink(info);
                that.info = info;
            } else {
                window.error(rep.msg);
            }
        }

        function doRecommendQueryRep(rep) {
            if (rep.code === window.CODE.SUCCESS) {
                that.recommendList = rep.data.list;
            } else {
                window.error(rep.msg);
            }
        }

        function doRewardQueryRep(rep) {
            if (rep.code === window.CODE.SUCCESS) {
                that.rewardList = rep.data.list;
            } else {
                window.error(rep.msg);
            }
        }

    },
    mounted(){

    },
    methods: {
        /**
         * 设置邀请链接
         * @private
         */
        _setInviteLink(info) {
            this.inviteLink = window.location.origin + window.location.pathname.replace('invite.html', 'register.html') + '?r=' + info.inviteCode;
        },

        _setDefaultData() {
            let info = {
                inviteCode: '123456789',    // 自己的邀请ID
                inviteCount: 5,     // 邀请的人数
                isReceiveReward: true,
                inviteUserRewardRecords: [   // 邀请好友的奖励
                    {
                        rewardAssetCode: 'ACT',
                        rewardAmount: 5
                    },
                    {
                        rewardAssetCode: 'USC',
                        rewardAmount: 4
                    },
                    {
                        rewardAssetCode: 'LTC',
                        rewardAmount: 1
                    }
                ],
                inviteActivityRewardConfigs: [   // 注册礼包
                    {
                        rewardAssetCode: 'ACT',
                        amount: 5
                    },
                    {
                        rewardAssetCode: 'ETH',
                        amount: 1
                    },
                    {
                        rewardAssetCode: 'SMC',
                        amount: 500
                    }
                ]
            };
            this._setInviteLink(info);
            this.info = info;

            this.recommendList = [
                {
                    inviteEmail: '12345@163.com',
                    createDate: 1516169110564
                },
                {
                    inviteEmail: '12345678@163.com',
                    createDate: 1516189110564
                }
            ];

            this.rewardList = [
                {
                    rewardAssetCode: 'ACT',
                    rewardAmount: 5,
                    createDate: 1516169110564
                },
                {
                    rewardAssetCode: 'USC',
                    rewardAmount: 50,
                    createDate: 1516189110564
                },
                {
                    rewardAssetCode: 'SMC',
                    rewardAmount: 500,
                    createDate: 1516184110564
                }
            ];
        },

        /**
         * 初始化基本信息
         * @private
         */
        _fetchInfoQuery() {
            this.isLoading = true;
            service.infoQuery({}).done((rep) => {
                this.isLoading = false;
                if (rep.code === window.CODE.SUCCESS) {
                    let info = rep.data;
                    // 生成link
                    this.inviteLink = window.location.origin + window.location.pathname + '?r=' + info.inviteCode;
                    this.info = info;
                } else {
                    window.error(rep.msg);
                }
            }).fail((rep) => {

            });
        },

        /**
         * 好友推荐列表
         * @private
         */
        _fetchRecommendQuery() {
            this.isLoading = true;
            service.recommendQuery({
                pageNo: 1,
                pageSize: DEFAULT_MAX_PAGE_SIZE
            }).done((rep) => {
                this.isLoading = false;
                if (rep.code === window.CODE.SUCCESS) {

                } else {
                    window.error(rep.msg);
                }
            }).fail((rep) => {

            });
        },

        copy($event){
            let target = $($event);
            lib.copy(target);
        }
    }
});
